package com.xyzq.webapp.action.sm;

import java.util.Date;
import java.util.List;

import org.appfuse.model.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import com.xyzq.model.sm.DepositoryBusiness;
import com.xyzq.service.sm.DepositoryBusinessManager;
import com.xyzq.webapp.action.BaseAction;

public class DepositoryBusinessAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1434972903214715592L;
	private DepositoryBusiness depositoryBusiness;
	private DepositoryBusinessManager depositoryBusinessManager;
	private List<DepositoryBusiness> depositoryBusinessList;
	private Long id;

	public String list() {
		this.depositoryBusinessList = this.depositoryBusinessManager.getAll();
		this.logUserAction("DepositoryBusiness", "read all list");
		return SUCCESS;
	}

	public String userList() {
		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		User user = (User) auth.getPrincipal();
		this.depositoryBusinessList = this.depositoryBusinessManager
				.findByUser(user);
		this.logUserAction("DepositoryBusiness", "read self list");
		return SUCCESS;
	}

	public String delete() {
		this.depositoryBusinessManager.remove(this.depositoryBusiness.getId());
		saveMessage(getText("depositoryBusiness.deleted"));
		this.logUserAction("DepositoryBusiness", "delete:"+this.getDepositoryBusiness());
		return SUCCESS;

	}

	public String editNew() {
		this.depositoryBusiness = new DepositoryBusiness();
		this.logUserAction("DepositoryBusiness", "ready input:"+this.getDepositoryBusiness());
		return SUCCESS;
	}

	public String edit() {
		if (this.getId() == null) {
			this.depositoryBusiness = new DepositoryBusiness();
			this.logUserAction("DepositoryBusiness", "ready input:"+this.getDepositoryBusiness());
		} else {
			this.depositoryBusiness = this.depositoryBusinessManager.get(this
					.getId());
			this.logUserAction("DepositoryBusiness", "ready edit:"+this.getDepositoryBusiness());
		}
		return SUCCESS;

	}

	public String save() {
		if (cancel != null) {
			return CANCEL;
		}
		if (delete != null) {
			return delete();
		}
		boolean isNew = (this.depositoryBusiness.getId() == null);
		
		if (isNew) {
			Authentication auth = SecurityContextHolder.getContext()
					.getAuthentication();
			User user = (User) auth.getPrincipal();
			if (this.depositoryBusinessManager.DidSub(user,
					this.depositoryBusiness.getWorkDate())) {
				String key = "depositoryBusiness.did";
				saveMessage(getText(key));
				return INPUT;
			}
			this.depositoryBusiness.setUser(user);
			this.depositoryBusiness.setSubTime(new Date());
		} else {
			DepositoryBusiness db = this.depositoryBusinessManager
					.get(this.depositoryBusiness.getId());
			this.depositoryBusiness.setUser(db.getUser());
			this.depositoryBusiness.setSubTime(db.getSubTime());
			this.depositoryBusiness.setModifyTime(new Date());
		}
		this.depositoryBusinessManager.save(depositoryBusiness);
		String key = (isNew) ? "depositoryBusiness.added"
				: "depositoryBusiness.updated";
		saveMessage(getText(key));
		if (!isNew) {
			this.logUserAction("DepositoryBusiness", "edit:"+this.getDepositoryBusiness());
			return INPUT;
		} else {
			this.logUserAction("DepositoryBusiness", "save:"+this.getDepositoryBusiness());
			return SUCCESS;
		}
	}

	public String saveNew() {
		if (cancel != null) {
			return CANCEL;
		}
		if (delete != null) {
			return delete();
		}
		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		User user = (User) auth.getPrincipal();
		if (this.depositoryBusinessManager.DidSub(user,
				this.depositoryBusiness.getWorkDate())) {
			String key = "depositoryBusiness.did";
			saveMessage(getText(key));
			return INPUT;
		}
		this.depositoryBusiness.setUser(user);
		this.depositoryBusiness.setSubTime(new Date());
		String key = "depositoryBusiness.added";
		saveMessage(getText(key));
		this.logUserAction("DepositoryBusiness", "save:"+this.getDepositoryBusiness());
		return SUCCESS;
	}

	public DepositoryBusiness getDepositoryBusiness() {
		return depositoryBusiness;
	}

	public DepositoryBusinessManager getDepositoryBusinessManager() {
		return depositoryBusinessManager;
	}

	public List<DepositoryBusiness> getDepositoryBusinessList() {
		return depositoryBusinessList;
	}

	public Long getId() {
		return id;
	}

	public void setDepositoryBusiness(DepositoryBusiness depositoryBusiness) {
		this.depositoryBusiness = depositoryBusiness;
	}

	public void setDepositoryBusinessManager(
			DepositoryBusinessManager depositoryBusinessManager) {
		this.depositoryBusinessManager = depositoryBusinessManager;
	}

	public void setDepositoryBusinessList(
			List<DepositoryBusiness> depositoryBusinessList) {
		this.depositoryBusinessList = depositoryBusinessList;
	}

	public void setId(Long id) {
		this.id = id;
	}


}
